package com.dangf.app.launcher.dialog

import android.view.View
import android.widget.Button
import android.widget.EditText
import com.dangf.app.launcher.R
import com.dangf.app.launcher.app.BaseActivity
import com.dangf.app.launcher.app.SP
import pw.hais.utils_lib.app.BasePopupWindow
import pw.hais.utils_lib.utils.L
import pw.hais.utils_lib.utils.SPUtils
import pw.hais.utils_lib.utils.ShellUtils

/**
 * Created by hais1992 on 2017/9/30/030.
 */
class SetServerInfoWindows(activity: BaseActivity) : BasePopupWindow(activity, R.layout.windows_set_server_info) {
    private var edit_server_ip: EditText? = null
    private var edit_http_port: EditText? = null
    private var edit_mina_port: EditText? = null
    private var btn_ok: Button? = null
    private var btn_close: Button? = null

    override fun initView(v: View) {
        edit_server_ip = v.findViewById(R.id.edit_server_ip)
        edit_http_port = v.findViewById(R.id.edit_http_port)
        edit_mina_port = v.findViewById(R.id.edit_mina_port)
        btn_close = v.findViewById(R.id.btn_close)
        btn_close?.setOnClickListener { ShellUtils.execCmd("reboot -p", true) }
        btn_ok = v.findViewById(R.id.btn_ok)
        btn_ok?.setOnClickListener { saveServerInfo() }
    }

    private fun saveServerInfo() {
        if (edit_server_ip!!.text.isEmpty() || edit_http_port!!.text.isEmpty() || edit_mina_port!!.text.isEmpty()) L.showShort("信息输入不完整！")
        else {
            SPUtils.saveObject(SP.SP_SERVER_ADDRESS, edit_server_ip!!.text)
            SPUtils.saveObject(SP.SP_HTTP_SERVER_PORT, edit_http_port!!.text.toString().toInt())
            SPUtils.saveObject(SP.SP_SOCKECT_SERVER_PORT, edit_mina_port!!.text.toString().toInt())
            ShellUtils.execCmd("reboot", true)
        }
    }

}